package indigo.utils 
{
	import flash.events.Event;
	/**
	 * An <code>ObjectPool</code> is used to create and return objects of the 
	 * <code>ObjectPool</code>'s type. It will only create new objects when the
	 * <code>ObjectPool</code> is empty. Otherwise it will return a previously
	 * instantiated object 
	 *
	 * @example
	 * <listing version="3.0">
	 * </listing>
	 * 
	 * @playerversion Flash 9
	 * @langversion ActionScript 3.0
	 * 
	 */
	public class ObjectPool implements IDisposable
	{
		/**
		 * Array that holds all inactive objects of the ObjectPool
		 */
		private var _objectList:Array;
		/**
		 * The object type that is used to create new objects
		 */
		private var _type:Class;

		/**
		 * Constructor
		 * @param The object type that is used to create new objects
		 * 
		 */
		public function ObjectPool( type:Class )
		{
			super( );
			this._type = type;
			this._objectList = new Array ();
		}

		
		/**
		 * Returns an Object of the ObjectPool's Object type. The object is 
		 * removed from the ObjectPool. When no object is found a new one is 
		 * created and returned.
		 * 
		 * @return Object from the ObjectPool
		 */
		public function checkOut():*
		{
			var myObject:*;
			
			// See if there are inactive objects of the same type
			if ( this._objectList.length > 0 )
			{
				myObject = this._objectList.pop( );
			}
			
			// Create a new object if no inactive objects were found and the 
			// totalSize of the objectpool hasn't reached its maximum
			else
			{
				myObject = new _type( );
			}
			
			return myObject;
		}

		
		/**
		 * Adds an Object of the ObjectPool's Object type to the ObjectPool
		 * @param Object to add to the Pool
		 * 
		 */
		public function checkIn( object:* ):void
		{
			if ( object is _type )
				this._objectList.push( object );
		}

		
		/**
		 * Empties the pool
		 * 
		 */
		public function empty():void
		{
			this._objectList = new Array( );
		}

		
		/**
		 * @inheritDoc
		 * 
		 */
		public function dispose( event:Event = null ):void
		{
			this._objectList = null;
			this._type = null;
		}
	}
}